Rely on typeshed/stubs for slice typing#21401
Conversation
|
cc @Dr-Irv |
|
Diff from mypy_primer, showing the effect of this PR on open source code: sympy (https://github.com/sympy/sympy)
+ sympy/stats/stochastic_process_types.py:1255: error: Unused "type: ignore" comment [unused-ignore]
pandas (https://github.com/pandas-dev/pandas)
+ pandas/tests/indexes/string/test_indexing.py:152: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:153: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:154: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:155: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:156: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:157: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:159: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:160: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:161: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:162: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:163: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:164: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:165: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:166: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/indexes/string/test_indexing.py:167: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:410: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:478: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:480: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:482: error: Unused "type: ignore" comment [unused-ignore]
+ pandas/tests/copy_view/test_indexing.py:485: error: Unused "type: ignore" comment [unused-ignore]
xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_dataarray.py:1327: error: Unused "type: ignore" comment [unused-ignore]
+ xarray/tests/test_dataarray.py:1346: error: Unused "type: ignore" comment [unused-ignore]
freqtrade (https://github.com/freqtrade/freqtrade)
- freqtrade/strategy/strategy_helper.py:107: error: Slice index must be an integer, SupportsIndex or None [misc]
- freqtrade/strategy/strategy_helper.py:108: error: Slice index must be an integer, SupportsIndex or None [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:119: error: Slice index must be an integer, SupportsIndex or None [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:120: error: Slice index must be an integer, SupportsIndex or None [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:135: error: Slice index must be an integer, SupportsIndex or None [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[int | str | str_ | Timestamp | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...] | Callable[[DataFrame], Never], int | str | str_ | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...]]" [index]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[tuple[IndexOpsMixin[Any, Any] | Series[builtins.bool] | ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any] | Sequence[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any]] | slice[Any, Any, Any], ...], Hashable]" [index]
- freqtrade/data/btanalysis/trade_parallelism.py:136: error: Slice index must be an integer, SupportsIndex or None [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:151: error: Slice index must be an integer, SupportsIndex or None [misc]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[int | str | str_ | Timestamp | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...] | Callable[[DataFrame], Never], int | str | str_ | tuple[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any], ...]]" [index]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Invalid index type "tuple[slice[Timestamp, str | bytes | date | timedelta | datetime64[date | int | None] | <7 more items> | None, None], str]" for "_LocIndexerFrame[DataFrame]"; expected type "tuple[tuple[IndexOpsMixin[Any, Any] | Series[builtins.bool] | ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]] | list[builtins.bool] | str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any] | Sequence[str | bytes | date | datetime | timedelta | <7 more items> | complex | integer[Any] | floating[Any] | complexfloating[Any, Any]] | slice[Any, Any, Any], ...], Hashable]" [index]
- freqtrade/data/btanalysis/trade_parallelism.py:152: error: Slice index must be an integer, SupportsIndex or None [misc]
|
|
|
|
For |
|
@Dr-Irv cc was more to use more precise slice type(s) if possible. Right now there are a bunch of plain |
In the class IndexSlice:
def __getitem__(self, arg):
return argso that How can we track when this is released? |
This will be included in v2.1.0 |
Fixes #2410
slicehas been generic in typeshed for more than a year now. Also all builtin collections have preciseslice[SupportsIndex | None]types for__getitem__(). So, I don't think we need to be "policing"sliceanymore, it should be really responsibility of the stub maintainers now.After some searching, I see there are still many plain
sliceannotations in the wild, so this will be a trade-off of false positives to false negatives, but I think this is a right trade-off, since status quo causes issues for pandas dataframes and similar APIs.I also considered some "intermediate" solutions, where we would still enforce
SupportsIndexin some situations, but can't find anything that wouldn't be overly complicated.